package com.han.jdbc.mysql.druid;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.junit.Test;

import com.alibaba.druid.pool.DruidDataSource;
import com.han.jdbc.mysql.uils.JDBCUtils;

/**
* @author hanpeng
* @version 创建时间：2020年7月20日 上午11:22:00
* @ClassName 类名称
* @Description 类描述
*/
public class GetDruid {
	@SuppressWarnings("resource")
	@Test
	public void getDataBase() throws IOException, SQLException {
		InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid/druid.properties");
		Properties pros = new Properties();
		pros.load(is);
		System.out.println(pros);
		DruidDataSource druidDataSource = new DruidDataSource();
		
		druidDataSource.setName("name");
		druidDataSource.setUsername(pros.getProperty("username"));
		druidDataSource.setUrl(pros.getProperty("url"));
		druidDataSource.setDriverClassName(pros.getProperty("driverClassName"));
		druidDataSource.setPassword(pros.getProperty("password"));
		DataSource dS=(DataSource)druidDataSource;
		
		Connection conn = dS.getConnection();
		String sql = "UPDATE `order` SET order_name=?";
		PreparedStatement ps = null;
		try {
			//1.获取数据库的连接
			conn =  dS.getConnection();
			//2.预编译sql语句，返回PreparedStatement的实例
			ps = conn.prepareStatement(sql);
			//3.填充占位符
			ps.setObject(1, "han");// 小心参数声明错误！！
			//4.执行
			//查询的话返回true，默认时匹配的行数 useAffectedRows=true 是更新的行数
			int num = ps.executeUpdate();
			
			System.out.println(num);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//5.资源的关闭
			JDBCUtils.closeResource(conn, ps);
			
		}

	}
	
}
